//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "HullWhiteForwardProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Processes/ForwardMeasureProcess1D.h>
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (Cephei::QL::Termstructures::IYieldTermStructure^ h, Double a, Double sigma) : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phHullWhiteForwardProcess = NULL;
#endif
        _Ch = safe_cast<CYieldTermStructure^> (h);
        _Ch->Lock();
        Handle<QuantLib::YieldTermStructure>& _h = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()); 
        QuantLib::Real _a = (QuantLib::Real)ValueHelper::Convert (a);
        QuantLib::Real _sigma = (QuantLib::Real)ValueHelper::Convert (sigma);
        _ppHullWhiteForwardProcess = new boost::shared_ptr<QuantLib::HullWhiteForwardProcess> (new QuantLib::HullWhiteForwardProcess ( _h,  _a,  _sigma ));
        SetForwardMeasureProcess1D (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (boost::shared_ptr<QuantLib::HullWhiteForwardProcess>& childNative, Object^ owner) : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteForwardProcess = NULL;
#endif
	_ppHullWhiteForwardProcess = &childNative;
    _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
}
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (QuantLib::HullWhiteForwardProcess& childNative, Object^ owner) : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteForwardProcess = NULL;
#endif
	_ppHullWhiteForwardProcess = new boost::shared_ptr<QuantLib::HullWhiteForwardProcess> (&childNative);
    _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
    _HullWhiteForwardProcessOwner = owner;
    _ForwardMeasureProcess1DOwner = owner;
}

Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (CHullWhiteForwardProcess^ copy) : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteForwardProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppHullWhiteForwardProcess = new boost::shared_ptr<QuantLib::HullWhiteForwardProcess> (copy->GetShared());
        _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
    }
}
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (System::Type^ t) : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteForwardProcess = NULL;
#endif
	if (!t->IsSubclassOf(CHullWhiteForwardProcess::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (QuantLib::Handle<QuantLib::HullWhiteForwardProcess>& childNative, Object^ owner)  : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
	_phHullWhiteForwardProcess = &childNative;
	_ppHullWhiteForwardProcess = &static_cast<boost::shared_ptr<QuantLib::HullWhiteForwardProcess>>(childNative.currentLink());
    _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
    _HullWhiteForwardProcessOwner = owner;
}
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (QuantLib::Handle<QuantLib::HullWhiteForwardProcess> childNative)  : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
	_phHullWhiteForwardProcess = &childNative;
	_ppHullWhiteForwardProcess = &static_cast<boost::shared_ptr<QuantLib::HullWhiteForwardProcess>>(childNative.currentLink());
    _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CHullWhiteForwardProcess::CHullWhiteForwardProcess (QuantLib::HullWhiteForwardProcess childNative)  : CForwardMeasureProcess1D(CHullWhiteForwardProcess::typeid)
{
#ifdef HANDLE
	_phHullWhiteForwardProcess = NULL;
#endif
	_ppHullWhiteForwardProcess = new boost::shared_ptr<QuantLib::HullWhiteForwardProcess> (new QuantLib::HullWhiteForwardProcess (childNative));
    _ppForwardMeasureProcess1D = new boost::shared_ptr<QuantLib::ForwardMeasureProcess1D> (boost::dynamic_pointer_cast<QuantLib::ForwardMeasureProcess1D> (*_ppHullWhiteForwardProcess));
}
#endif

Cephei::QL::Processes::CHullWhiteForwardProcess::~CHullWhiteForwardProcess ()
{
    if (_ppHullWhiteForwardProcess != NULL)
    {
	    delete _ppHullWhiteForwardProcess;
        _ppHullWhiteForwardProcess = NULL;
    }
}
Cephei::QL::Processes::CHullWhiteForwardProcess::!CHullWhiteForwardProcess ()
{
    if (_ppHullWhiteForwardProcess != NULL)
    {
	    delete _ppHullWhiteForwardProcess;
    }
}
QuantLib::HullWhiteForwardProcess& Cephei::QL::Processes::CHullWhiteForwardProcess::GetReference ()
{
    if (_ppHullWhiteForwardProcess == NULL) throw gcnew NativeNullException ();
	return **_ppHullWhiteForwardProcess;
}
boost::shared_ptr<QuantLib::HullWhiteForwardProcess>& Cephei::QL::Processes::CHullWhiteForwardProcess::GetShared ()
{
    if (_ppHullWhiteForwardProcess == NULL) throw gcnew NativeNullException ();
	return *_ppHullWhiteForwardProcess;
}
QuantLib::HullWhiteForwardProcess* Cephei::QL::Processes::CHullWhiteForwardProcess::GetPointer ()
{
    if (_ppHullWhiteForwardProcess == NULL) throw gcnew NativeNullException ();
	return &**_ppHullWhiteForwardProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::HullWhiteForwardProcess>& Cephei::QL::Processes::CHullWhiteForwardProcess::GetHandle ()
{
	if (_phHullWhiteForwardProcess == NULL)
	{
		_phHullWhiteForwardProcess = new Handle<QuantLib::HullWhiteForwardProcess> (*_ppHullWhiteForwardProcess);
	}
	return *_phHullWhiteForwardProcess;
}
#endif
bool Cephei::QL::Processes::CHullWhiteForwardProcess::HasNative () 
{
	return (_ppHullWhiteForwardProcess != NULL);
}

Double Cephei::QL::Processes::CHullWhiteForwardProcess::A::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->a ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Alpha (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->alpha ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::B (Double t, Double T2)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Time _T2 = (QuantLib::Time)ValueHelper::Convert (T2);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->B ( _t,  _T2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Diffusion (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->diffusion ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Drift (Double t, Double x)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->drift ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Expectation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->expectation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::M_T (Double s, Double t, Double T2)
{
    try
    {
        QuantLib::Real _s = (QuantLib::Real)ValueHelper::Convert (s);
        QuantLib::Real _t = (QuantLib::Real)ValueHelper::Convert (t);
        QuantLib::Real _T2 = (QuantLib::Real)ValueHelper::Convert (T2);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->M_T ( _s,  _t,  _T2 );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Sigma::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->sigma ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::StdDeviation (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->stdDeviation ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::Variance (Double t0, Double x0, Double dt)
{
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0);
        QuantLib::Real _x0 = (QuantLib::Real)ValueHelper::Convert (x0);
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->variance ( _t0,  _x0,  _dt );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CHullWhiteForwardProcess::X0::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppHullWhiteForwardProcess)->x0 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Processes::IHullWhiteForwardProcess^ Cephei::QL::Processes::CHullWhiteForwardProcess_Factory::Create (Cephei::QL::Termstructures::IYieldTermStructure^ h, Double a, Double sigma)
{
    return gcnew CHullWhiteForwardProcess ( h,  a,  sigma);
}
